package eclipse.server;
import eclipse.lib.player.*;
import eclipse.lib.scripting.*;
import eclipse.lib.sockets.*;
import eclipse.server.threadpool.*;
import eclipse.util.*;
import eclipse.util.io.*;
/** Class that loads all of the Data for the Server. */
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public final class Loader 
{
	/** Initializes the Socket connections for the Server */
	public static final void TCPInit()
	{
		//Set the max players to equal the client number
		Database.MAXPLAYERS	= Database.CLIENTNUM;
		//set the array of clients to be the number of clients
		Database.client = new ClientSocket[Database.CLIENTNUM];
		//set the array of player to be the number of clients
		Database.player = new Player[Database.CLIENTNUM];
		//create the server object
		Database.server = new ServerSocket(Database.PORTNUM, Database.client, Database.CLIENTTIMEOUT);
		//scroll through each player
		for(int i = 0; i < Database.MAXPLAYERS; i++)
		{
			//create a player instance for each Array.
			//TODO: possibly remove this because I prefer the way Clients work
			Database.player[i] = new Player();
		}
	}
	/** Processes any overwrites that might have been sent at startup */
	public static final void ArgsProcess(String args[])
	{
		String value;
		//scan through the list
		for(int scan = 0; scan < args.length; scan++)
		{			
			//check if the argument changes the interval for net processes
			if(args[scan].toLowerCase().startsWith("-netint="))
			{
				//structure the String
				value = args[scan].toLowerCase().replace("-netint=", "").trim();
				//get the value from the String
				Database.NETPROCESSINTERVAL = Integer.parseInt(value);
				//print the value
				System.out.println("Net Process Thread interval set to: " + value);
			}
			//check if the argument changes the number of clients
			else if(args[scan].toLowerCase().startsWith("-clientnum="))
			{
				//structure the String
				value = args[scan].toLowerCase().replace("-clientnum=", "").trim();
				//get the value from the String
				Database.CLIENTNUM = Integer.parseInt(value);
				//print the value
				System.out.println("Client number set to: " + value);
			}
			//check if the argument changes the port 
			else if(args[scan].toLowerCase().startsWith("-portnum="))
			{
				//structure the String
				value = args[scan].toLowerCase().replace("-portnum=", "");
				//get the value from the String
				Database.PORTNUM = Integer.parseInt(value);
				//print the value
				System.out.println("Port number set to: " + value);
			}
			//check if the argument changes the maximum players
			else if(args[scan].toLowerCase().startsWith("-maxplayers="))
			{
				//structure the String
				value = args[scan].toLowerCase().replace("-maxplayers=", "");
				//get the value from the String
				Database.CLIENTNUM = Integer.parseInt(value);
				//print the value
				System.out.println("MAXPLAYERS number set to: " + value);
			}
			//check if the argument changes the maximum items
			else if(args[scan].toLowerCase().startsWith("-maxitems="))
			{
				//structures the String
				value = args[scan].toLowerCase().replace("-maxitems=", "");
				//get the value form the String
				Database.MAXITEMS = Integer.parseInt(value);
				//print the value
				System.out.println("MAXITEMS number set to: " + value);
			}
			//check if the argument changes the maximum classes
			else if(args[scan].toLowerCase().startsWith("-maxclasses="))
			{
				//structure the String
				value = args[scan].toLowerCase().replace("-maxclasses=", "");
				//get the value from the String
				Database.MAXCLASSES = Integer.parseInt(value);
				//print the value
				System.out.println("MAXCLASSES number set to: " + value);
			}
			//check if the argument changes the maximum npcs
			else if(args[scan].toLowerCase().startsWith("-maxnpcs="))
			{
				//structure the String
				value = args[scan].toLowerCase().replace("-maxnpcs=", "");
				//get the value from the String
				Database.MAXNPCS = Integer.parseInt(value);
				//print the value
				System.out.println("MAXNPCS number set to: " + value);			
			}
			//check if the argument changes the maximum spells
			else if(args[scan].toLowerCase().startsWith("-maxspells="))
			{
				//structure the String
				value = args[scan].toLowerCase().replace("-maxspells=", "");
				//get the value from the String
				Database.MAXSPELLS = Integer.parseInt(value);
				//print the value
				System.out.println("MAXSPELLS number set to: " + value);
			}
			//check if the argument changes the maximum maps
			else if(args[scan].toLowerCase().startsWith("-maxmaps="))
			{
				//structure the String
				value = args[scan].toLowerCase().replace("-maxmaps=", "");
				//get the value from the String
				Database.MAXMAPS = Integer.parseInt(value);
				//print the value
				System.out.println("MAXMAPS number set to: " + value);
			}
			//check if the argument changes the maximum shops
			else if(args[scan].toLowerCase().startsWith("-maxshops="))
			{
				//structure the String
				value = args[scan].toLowerCase().replace("-maxshops=", "");
				//get the value from the String
				Database.MAXSHOPS = Integer.parseInt(value);
				//print the value
				System.out.println("MAXSHOPS number set to: " + value);
			}
			//check if the argument changes the client timeout
			else if(args[scan].toLowerCase().startsWith("-timeout="))
			{
				//structure the String
				value = args[scan].toLowerCase().replace("-timeout=", "");
				//get the value from the String
				Database.CLIENTTIMEOUT = Integer.parseInt(value);
				//print the value
				System.out.println("Client timeout set to: " + value);
			}
		}
	}	
	
	/** Launches the thread database */
	public static final void ThreadInit()
	{
		//create the Thread collector
		Database.threadCache = new ThreadCache();
	}
	
	/** Loads the Scripting Engine */
	public static final void ScriptInit()
	{	
		//Allow Scripting to access JavaScript
		JScript.makeObject("JavaScript", new JScript());
		//Allow Scripting to access SadScript
		JScript.makeObject("SadScript", new SadScript());
		//Allow Scripting to access IOTools
		JScript.makeObject("IOTools", new IOTools());
		//Allow Scripting to access ObjectFactory
		JScript.makeObject("ObjectFactory", new ObjectFactory());
		
		//Create the "importScript" function as a wrapper to the JavaScript function importScript.
		JScript.addProcedure
		(
				"function importScript(script)" +
				"{" +
				"	JavaScript.importScript(SadScript.getERL(script));" +
				"}"
		);
		//Create the global used for the svn
		JScript.addProcedure("var svn = \"" + Database.SVN + "\";");
		//load the main.txt Script into the database.
		JScript.importScript(new ERL("Scripts/main.txt").openStream());		
	}
	
	/**
	 * The buildEnvironment command confirms that each file/directory that the Server requires exists.
	 * If the Server cannot find the file/directory, it attempts to either create the file or download it from the svn.
	 */
	public static final void buildEnvironment()
	{
		//confirm that the directory "Accounts" exists
		IOTools.makeDir("Accounts");
		//confirm that the directory "HTML" exists
		IOTools.makeDir("HTML");
		//confirm that the directory "Scripts" exists
		IOTools.makeDir("Scripts");
		//confirm that the directory "Scripts/plugins" exists
		IOTools.makeDir("Scripts/plugins");
		
		//confirm that main.txt exists
		if(!IOTools.fileExists("Scripts/main.txt"))
		{
			//output that the Server is downloading main.txt
			System.out.println("Downloading 'main.txt'");
			//Download the main.txt because it didnt exist
			IOTools.download(Database.SVN + "Scripts/main.txt", "Scripts/main.txt");
		}
		
		//confirm that server.txt exists
		if(!IOTools.fileExists("Scripts/server.txt"))
		{
			//output that the Server is downloading server.txt
			System.out.println("Downloading 'server.txt'");
			//Download the server.txt because it didnt exist
			IOTools.download(Database.SVN + "Scripts/server.txt", "Scripts/server.txt");
		}
		
		//confirm that the html plugin exists
		if(!IOTools.fileExists("Scripts/plugins/html.txt"))
		{
			//ouput that the Server is downloading the html plugin
			System.out.println("Downloading 'plugins/html.txt'");
			//Download the html plugin because it didnt exist
			IOTools.download(Database.SVN + "Scripts/plugins/html.txt", "Scripts/plugins/html.txt");
		}
		
		//confirm that data.ini exists
		if(!IOTools.fileExists("data.ini"))
		{
			//output that the Server is downloading data.ini
			System.out.println("Downloading 'data.ini'");
			//Download data.ini because it didnt exist
			IOTools.download(Database.SVN + "Config/data.ini", "data.ini");			
		}
		
		//confirm that motd.ini exists
		if(!IOTools.fileExists("motd.ini"))
		{
			//output that the Server is downloading motd.ini
			System.out.println("Downloading 'motd.ini'");
			//Download the motd file because it didnt exist
			IOTools.download(Database.SVN + "Config/motd.ini", "motd.ini");
		}
	}
	public static final void shutdown()
	{
		Database.console.println("Shutting server down...", 32);
		System.exit(0);
	}
}
